// pages/select_identity/select_identity.js
const app = getApp();
const request = require('../../utils/request.js');
const url = require('../../utils/url.js');
const cookie = require('../../utils/cookie.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    token: '',
    userinfo: '',
	login:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this;
    cookie.getCookie('userInfo', function (res) {
      if (res) {
        var etime = Date.parse(new Date()) / 1000;
		console.log(res)
		 that.getUserInfo();
        if (etime < res.expiretime) {
          that.setData({
            login: 1,
            token: res.token
          })
          
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  gouserindex() {
	console.log('login:'+this.data.login);
    wx.setStorageSync('lastModePage', 1)
    wx.navigateTo({
     url:"../authorization/authorization"
   })
  },
  checkUser: function (groupid) {
	    var that = this;
	 
	if(groupid==2)
	{
		if (that.data.userinfo.group_id == 2 && that.data.userinfo.checkinfo == 1) {
		  wx.setStorageSync('lastModePage', 2)
		  wx.requestSubscribeMessage({
		    tmplIds: [
		     'q9Js-vQrnKFfxtsIYMrYF6CQgNNsPITiZtcznzk8P-M',
		     'WCOeBpOwto_yU4ZM-hUmQePvbfY_lxKyaFK5Yi7nzao',		   
		     'SOnJPXow5f0XddOr1jQ-3wdd6V1SifOHJMdQtPbJNo0'
		    ],
		    success: (res) => {
		      wx.navigateTo({
		        url: '../server_end/index/index'
		      })
		    },
		    fail: (res) => {
		      wx.navigateTo({
		        url: '../server_end/index/index'
		      })
		    },
		  })
		
		} else if (that.data.userinfo.group_id == 2 && that.data.userinfo.checkinfo == 0) {
		  wx.showToast({
		    title: "请等待平台审核!",
		    icon: "none"
		  })
		} else if (that.data.userinfo.group_id == 2 && that.data.userinfo.checkinfo == 2) {
			
		  wx.navigateTo({
		    url: '../server_end/apply_server/apply_server'
		  })
		}
		 else {
		  
		  wx.navigateTo({
		    url: '../server_end/apply_server/apply_server'
		  })
		}
	}
	if(groupid==5)
	{
		if (that.data.userinfo.group_id == 5 && that.data.userinfo.checkinfo == 1) {
		  wx.setStorageSync('lastModePage', 5)
		  wx.requestSubscribeMessage({
		    tmplIds: [
		     'q9Js-vQrnKFfxtsIYMrYF6CQgNNsPITiZtcznzk8P-M',
		     'WCOeBpOwto_yU4ZM-hUmQePvbfY_lxKyaFK5Yi7nzao',		   
		     'SOnJPXow5f0XddOr1jQ-3wdd6V1SifOHJMdQtPbJNo0'
		    ],
		    success: (res) => {
		      wx.navigateTo({
		        url: '../server_end/index_expert/index_expert'
		      })
		    },
		    fail: (res) => {
		      wx.navigateTo({
		        url: '../server_end/index_expert/index_expert'
		      })
		    },
		  })
		
		} else if (that.data.userinfo.group_id == 5 && that.data.userinfo.checkinfo == 0) {
		  wx.showToast({
		    title: "请等待平台审核!",
		    icon: "none"
		  })
		} else if (that.data.userinfo.group_id == 5 && that.data.userinfo.checkinfo == 2) {
			
		  wx.navigateTo({
		    url: '../server_end/apply_expert/apply_expert'
		  })
		}
		 else {
		  
		  wx.navigateTo({
		    url: '../server_end/apply_expert/apply_expert'
		  })
		}
	}
    
  },
  gouserinfo:function(e){
	  var that = this;
	  
	  wx.requestSubscribeMessage({
	    tmplIds: [
	    'WCOeBpOwto_yU4ZM-hUmQePvbfY_lxKyaFK5Yi7nzao',		   
	    'SOnJPXow5f0XddOr1jQ-3wdd6V1SifOHJMdQtPbJNo0',
	    'Z-OFKRd2aAOX20azHmd6D30cXIdIGaW1jX-QWEHieHc'
	    ],
	    success: (res) => {
	      that.getUserInfo(e)
	    },
	    fail: (res) => {
	       that.getUserInfo(e)
	    },
	  })
  },
  getUserInfo: function (e) {
    var that = this;
	var groupid =0;
	if(e!=undefined)
	{
		groupid = e.currentTarget.dataset.groupid;		
	}
    let param = {
      scoure: 'wxapp',
      token: that.data.token
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.getuserinfo, param, url.REQUEST_METHOD_GET).then((res) => {
		console.log(res.data);
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      if (res.data.code == 1) {
        that.setData({
          userinfo: res.data.data
        })
		if(groupid==0)
		{
			groupid=res.data.data.group_id
		}
		 that.checkUser(groupid);
      }
	  else{
		  wx.showToast({
		    title: "请登录后操作",
		    icon: "none"
		  })
		  wx.navigateTo({
		    url: '/pages/authorization/authorization'
		  })
		  
	  }
    })
  }
})